Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
How to strongly type "Current" when implementing IEnumerator

We are implementing IEnumerator and IEnumerable in our class

    static void Main(string[] args)
    {
       

        ParsingFile data = new ParsingFile();

        foreach (var line in data) // Calls the current, the implicit type of line is object but we want is as a DataRow
        {
            //call MoveNext
        }

        
    }

public class ParsingFile : IEnumerator , IEnumerable 
{

    object IEnumerator.Current //need the object to be of type DataRow to avoid casting in foreach
    {
       //return a DataRow
    }


    public IEnumerator GetEnumerator()
    {
        return this;
    }


    public bool MoveNext()
    {
    //Do something
    }

}

The return type of current is object but want to explicitly return DataRow. Please let us know what will be the approach to achieve this?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Quite honestly, unless you have very particular requirements, you're better off using an iterator block here, i.e. you just write:

public class ParsingFile : IEnumerable<DataRow>
{
    // use an iterator block for the typed API
    public IEnumerator<DataRow> GetEnumerator()
    {
        //Do something that involves, perhaps in a loop 
        // yield return someValue;
        // and/or
        // yield break;
    }

    // use explicit implementation for the untyped API
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

You absolutely can implement your own strong-typed enumerator, which may (although doesn't strictly need to) mean implementing IEnumerator<T>, but there are a lot of gotchas (such as allowing independent concurrent enumeration) that make it safer to let the compiler do the hard work.

over 4 years ago · Santiago Trujillo Relatório

0

Instead of implementing an IEnumerator, which it's interface specifies that current is an object, you have to implement IEnumerator<T>

public class ParsingFile : IEnumerator<DataRow>
{
    public DataRow Current { get; }

    // rest of code ommited for brevity
}
over 4 years ago · Santiago Trujillo Relatório

0

As mentioned by jon skeet, implement IEnumerable<DataRow> or IEnumerator<DataRow> instead.

I would suggest to never implement both IEnumerator and IEnumerable by the same class. Consider the following:

  var myParsingFile = ...
  var dr1 = myParsingFile.First();
  var dr2 = myParsingFile.First();

In your case the dr1 and dr2 would be different objects, and that is just very confusing.

You may also lookinto iterator blocks for an easier way to make enumerators.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda